//
//  TextViewFieldViewController.swift
//  swift各类控件集锦
//
//  Created by iosdev1 on 2016/12/29.
//  Copyright © 2016年 zyy. All rights reserved.
//

import UIKit

class TextViewFieldViewController: ZyBaseViewController, UITextFieldDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        let arrKeyBoard = [UIKeyboardType.default, UIKeyboardType.asciiCapable, UIKeyboardType.numbersAndPunctuation,
                                     UIKeyboardType.URL,UIKeyboardType.numberPad,UIKeyboardType.phonePad,
                                     UIKeyboardType.namePhonePad,UIKeyboardType.emailAddress,UIKeyboardType.decimalPad,
                                     UIKeyboardType.twitter,UIKeyboardType.webSearch]
        
//        UIReturnKeyDefault,
//        UIReturnKeyGo,
//        UIReturnKeyGoogle,
//        UIReturnKeyJoin,
//        UIReturnKeyNext,
//        UIReturnKeyRoute,
//        UIReturnKeySearch,
//        UIReturnKeySend,
//        UIReturnKeyYahoo,
//        UIReturnKeyDone,
//        UIReturnKeyEmergencyCall,
        
        let ReturnKey = [UIReturnKeyType.default,UIReturnKeyType.go,UIReturnKeyType.google,
                                  UIReturnKeyType.join,UIReturnKeyType.next,UIReturnKeyType.route,
                                  UIReturnKeyType.search,UIReturnKeyType.send,UIReturnKeyType.yahoo,
                                  UIReturnKeyType.done,UIReturnKeyType.emergencyCall]
        
        let arrPlace = ["右边按钮覆盖删除", "2","3", "4","5", "6","7", "8","9", "10","11"]
        
        
        for i in 0 ..< 11 {
            let tfd = UITextField.init(frame: CGRect(x:10.0, y:65.0 + CGFloat(i * 50), width:300, height:40))
            self.view.addSubview(tfd)

            //            键盘样式
            tfd.keyboardType = arrKeyBoard[i]
//        背景颜色
//            tfd.backgroundColor = UIColor.green
//            设置背景
            tfd.background = UIImage.init(named: "girl")
            tfd.disabledBackground = UIImage.init(named: "dm1")
            
//            设置提示文本 //当输入框没有内容时，水印提示 提示内容
            tfd.placeholder = arrPlace[i]
            //设置输入框内容的字体样式和大小
            tfd.font = UIFont.init(name: "Arial", size: 15.0)
            //设置字体颜色
            tfd.textColor = UIColor.red
            
            //输入框中是否有个叉号，在什么时候显示，用于一次性删除输入框中的内容
            tfd.clearButtonMode = UITextFieldViewMode.always
//            typedef enum {
//                UITextFieldViewModeNever, 重不出现
//                UITextFieldViewModeWhileEditing, 编辑时出现
//                UITextFieldViewModeUnlessEditing, 除了编辑外都出现
//                UITextFieldViewModeAlways 一直出现
//            } UITextFieldViewMode;
            
            //输入框中一开始就有的文字
//            tfd.text = "开始"
            
            //每输入一个字符就变成点 用语密码输入
//            tfd.isSecureTextEntry = true
            
            //是否纠错
            tfd.autocorrectionType = UITextAutocorrectionType.no
            
            //再次编辑就清空
            tfd.clearsOnBeginEditing = true
            //内容对齐方式
            tfd.textAlignment = NSTextAlignment.center
          //内容的垂直对齐方式 UITextField继承自UIControl,此类中有一个属性
            tfd.contentVerticalAlignment = UIControlContentVerticalAlignment.bottom
            
            //设置为true时文本会自动缩小以适应文本窗口大小.默认是保持原来大小,而让长文本滚动
            tfd.adjustsFontSizeToFitWidth = true
            
            //设置自动缩小显示的最小字体大小
            tfd.minimumFontSize = 20.0
            
            //首字母是否大写
            tfd.autocapitalizationType = UITextAutocapitalizationType.none
            
//            设置返回样式 return键变成什么键
            tfd.returnKeyType = ReturnKey[i]//要显示中文，还得设置info.plist中 Localization native development region  为 cn
            //键盘外观
            tfd.keyboardAppearance = UIKeyboardAppearance.dark
            
            //设置边框样式，只有设置了才会显示边框样式
            tfd.borderStyle = UITextBorderStyle.roundedRect
            
            //设置代理 用于实现协议
            tfd.delegate = self
            
            
            //最右侧加图片是以下代码 左侧类似,x,y 不起作用的
            
            let btnRight = UIButton.init(frame: CGRect(x:0, y:1000, width:45, height:20))
            btnRight.setTitle("右边", for: UIControlState.normal)
            btnRight.setTitleColor(UIColor.blue, for: UIControlState.normal)
            btnRight.backgroundColor = UIColor.cyan
            btnRight.addTarget(self, action: #selector(btnRightClick(btn:)), for: UIControlEvents.touchUpInside)
            tfd.rightView = btnRight
            tfd.rightViewMode = UITextFieldViewMode.always

            
            let btnLeft = UIButton.init(frame: CGRect(x:1000, y:10, width:45, height:20))
            btnLeft.setTitle("左边", for: UIControlState.normal)
            btnLeft.setTitleColor(UIColor.blue, for: UIControlState.normal)
            btnLeft.backgroundColor = UIColor.yellow
            btnLeft.addTarget(self, action: #selector(btnLeftClick(btn:)), for: UIControlEvents.touchUpInside)
            tfd.leftView = btnLeft
            tfd.leftViewMode = UITextFieldViewMode.always
            
            
            //按return键键盘往下收 becomeFirstResponder
        }
        
    }
    
    func btnRightClick(btn:UIButton) -> Void {
        print("右边")
    }
    
    func btnLeftClick(btn:UIButton) -> Void {
        print("左边")

//        1、[view endEditing:YES]  这个方法可以让整个view取消第一响应者，从而让所有控件的键盘隐藏。
//        2、[textFiled resignFirstResponder] 这个则是比较常用的让某个textFiled的键盘隐藏。
    }

    func textFieldDidEndEditing(_ textField: UITextField) {
        
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder() //主要是[receiver resignFirstResponder]在哪调用就能把receiver对应的键盘往下收
        return true
    }

    
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
